package com.xysh.aes;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;

/**
 * AES工具类
 * Created by lucio on 2019/7/15.
 */

public class AesKit {

    private static final Charset UTF_8 = Charset.forName("UTF-8");

    private AesKit() {}

    public static byte[] encrypt(String content, String aesTextKey) {
        return encrypt(content.getBytes(UTF_8), aesTextKey.getBytes(UTF_8));
    }

    public static String decryptToStr(byte[] content, String aesTextKey) {
        return new String(decrypt(content, aesTextKey.getBytes(UTF_8)), UTF_8);
    }

    public static String decryptAndDecodeToStr(String content, String aesTextKey) {
        try {
            byte[] encryptBytes = HexKit.decodeHex(content.toCharArray());
            String encodeData = AesKit.decryptToStr(encryptBytes, aesTextKey);
            return URLDecoder.decode(encodeData, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException();
        }
    }

    public static String encryptAndEncodeToStr(String content, String aesTextKey) {
        try {
            String encodeStr = URLEncoder.encode(content, "UTF-8");
            byte[] aesByte = encrypt(encodeStr, aesTextKey);
            char[] encode = HexKit.encodeHex(aesByte, true);
            return String.valueOf(encode);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException();
        }
    }

    public static byte[] encrypt(byte[] content, byte[] aesKey) {
        return encrypt(content, aesKey, aesKey);
    }

    public static byte[] encrypt(byte[] content, byte[] aesKey, byte[] aesIv) {
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            SecretKeySpec keySpec = new SecretKeySpec(aesKey, "AES");
            cipher.init(Cipher.ENCRYPT_MODE, keySpec);
            return cipher.doFinal(content);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    public static byte[] decrypt(byte[] encrypted, byte[] aesKey) {
        return decrypt(encrypted, aesKey, aesKey);
    }

    public static byte[] decrypt(byte[] encrypted, byte[] aesKey, byte[] aesIv) {
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            SecretKeySpec key_spec = new SecretKeySpec(aesKey, "AES");
            cipher.init(Cipher.DECRYPT_MODE, key_spec);
            return cipher.doFinal(encrypted);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    public static void main(String[] args) {

        String sha256key = "w88g7z";
        String aesKey = "57m6Y3h9V57fU36U";

        String jsonStr = "{\"order_id\":\"20190715202522up0001\",\"apply_id\":\"\",\"first_card\":\"\",\"state\":\"0\",\"state_time\":\"20180208123046\",\"is_union_pay_trade\":\"0\",\"union_pay_trade_time\":\"\",\"card_number\":\"0\",\"reserved\":\"0\"}";
        String encodeStr = encryptAndEncodeToStr(jsonStr, aesKey);
        //String signature = EncryptKit.sha256Encrypt(encodeStr.concat(sha256key));
        //System.out.println("encodeStr -->  " +  encodeStr);
        //System.out.println("signature -->  " +  signature);

        encodeStr = "ec5408405a1ed6d1250f03cecce6e001fc86683e8d87b78591eb66fe026316adb09c06097100e6390c9388a1e39ac38738faf3fd59b360f5fb6db192476b1e905611f57f9d59c6f69435c233b22006784999540bec985312c1d61d8c358bf22fe2a857a22cfb29fc34f19e245891142835d7951c0867772b9b2b30f28e8280aaf75a3171ee5ba32a7edd6366c6b240fa2d09b98113dd6a83f7773cff5a514936a5a90504f9f33f25b10df8a152e0ee30c0218c3bfba1dde83e70eb86c97aa43bcdc417b63d522c99d7126f4d9b64566571f4615959ee9c9c2f12f791556716d173fba50506a6e73443219393e1708396a6a3d45c468fa2d0117a3fd92b396a30a5968cea5511f2ff21ad8d37951c5a847b6522e19e7e94d0094700fe001e72e062e2465b8b7e6fe4afaef65bed0651900f2c3dc8a41bf840939f99e5143086cab05f3c1090eb651c09d39566cf2f0ecbfb56e8d72c011bb1b0c378752a50b777bdbccb0f0d39080f248cf079f1a7c46b80094bed8df04bdb689ba9d05d8f7a70d4eacce4e47519e758d96030634a0f6ace905f23bbd4c957ef66cd309fb4ba98d5bcb5eaea06d28bd0c4ced30875865451c5ca43513cd220cc1a9ffa4d8cdc0d3b9cca8a9d4c25f2ccb053187d9abc5472ae5c77bb19d0dc93838db3ffdbb18fff93bbd9cf94b9441af98fdba80c75499b805605b9e01fe4dacbb30cab3561b4e7177797cc763e289db12f576b91e5a43a4d47bfee3a452401f2e4ff6c9ba73688ce81cf903f58e8963e3e1cbc6629835993745b72768f639049c58066575b1c8390e6b916202666ff3257296bfe47d3471dca36bdf93c33b71ba7a5dccec2a75de267e07664fa58bd1a91ed7f745eefbd1533892fda96702b084cf00c9a3167089a58d4f0e79bd8c1537bff7b3d049aa07e216bc38b6d330f90e7f982055122ed0cd6b9b52ad980ab34d5db1acd2863c216c609c7aa986cd935abfd12b4db84e1b1bb9e7a0183fb2708776563b9c72d3b9cca8a9d4c25f2ccb053187d9abc54555bcc894bf71df2c3b3d014f5b553258390e6b916202666ff3257296bfe47d34488fdd490e47a33a447772d688e60aa4235425a1a3f1ac88a7702eaf603225d2d26332c4f745a8d1483f837c13e9cbdad1fa1d2768752ed8815c6f241a430c5a4000a038ef58d10548ae7adfcaf5239b35411bb1793492c353c065237e5ff55c2eb21ef4cba0a19858a1484d44e0b8d73ffd37d51f64151b968f68b844e1c35bec75f30f393627ed095feb08826a18d54090127d97d81c9879659142ce4d4ee092d7ce814376eaede08c296b2e93419193d30b3ec0d7c4aa4038be50f717a1ab0c8e52cd5a9143dce4a80986499cf9c5b7bb0af17710d9cc4adc95734d07eb80ca96b18ad13399919b4a3c3e5479727f96a76ae98c4731cbc44f3829a5e1e800d77620ba5c0dfda91a4ad9ed4f5ac7e97021f5e8517155d065f5c3878c7bdd60d77620ba5c0dfda91a4ad9ed4f5ac7e13fb94cab1677906a908fc7bca99ecd8235d7ffd231125917a7bd2f63b9c6783f376fc81fb4534750292e348697bd2e1b215c78e9984ce90313712b8e50cc5dd8783942b44fdf6e1b78eaa357689476a957c81df7fb3648339f765989f21addb13bf1a71ebbc6bc95979c2dea5778a0ef376fc81fb4534750292e348697bd2e15adac3e7c55ffb7490aa0e93eb584c148783942b44fdf6e1b78eaa357689476a957c81df7fb3648339f765989f21addbe78ba496a0001c09dab86a16d81577ef957b862c273f24119528344f01e5e22d6d2d76c3c84457f82b594200c846d3ce0ca96b18ad13399919b4a3c3e5479727fd94d28330753660166c881dac230e078390e6b916202666ff3257296bfe47d3c9574f7503dd9419b06ffd8aca62c047c7e694855ac0f1078760fb2d74b7ebc79457f453da25ec916a50e3636c03a66ab91173727d69e71d82a892ec50bffdb79f5e3fb5e602554a923a1e6f8eeb43647f1292fbe14fb79a23cb817c2e18c90c4a9d8c38ac55e12d444013003452b80a000726012c17aebe9d86ccda906a3622c31d90ab2b0b05c179ec0f956f9afe041573c59565943ac1019c464efd4f6022f7bbf69ec1ee4a5acfb44844dd48c0b18390e6b916202666ff3257296bfe47d37760e7a4a8b702a58c87287063b0afc00d77620ba5c0dfda91a4ad9ed4f5ac7e313a8cc41c6a2a19564ddcd94578348c72dce5e4d3b17c7470d1c3dc909ead78da14143810adf3efc8ffcf11c629045806acf35501b7bbf380c0fef378a955c1dba92fdbe81c804a371d913fae703b8758c67742c4ac233f1ca36297f262d97150369f9394e2f1a795150d0c960d89a2712b03851fe64d3af3a895bcab7a15b731e9d42624aa62fe4a0f140909a562b8";
        String decodeStr = decryptAndDecodeToStr(encodeStr, aesKey);
        System.out.println(decodeStr);
    }
}
